#ifndef N1THREADSINGLETON_2006_10_29
#define N1THREADSINGLETON_2006_10_29

#include "N1Lib.h"

class N1Dictionary;

extern N1Dictionary* m_dict;

template<typename T>
class N1ThreadSingleton { 

    static N1Dictionary* getDictionary()
    {
        if(!m_dict)
        {
            m_dict = new N1Dictionary();
        }
        return m_dict;
    }

    void create(N1Object *obj)
    {
        N1Dictionary *dict = getDictionary();
        N1Number *n1 = new N1Number( N1Thread::currentThreadId() );
        dict->setObject(n1, obj);
    }

public:
    N1ThreadSingleton(N1Object* v) {
        create(v);
    };

    N1ThreadSingleton(void* v) {
        create((N1Object*)v);
    };

    N1ThreadSingleton(int v) {
        create((N1Object*)v);
    };

    operator T() {
        N1Dictionary *dict = getDictionary();
        N1Number n1(N1Thread::currentThreadId());
        return (T)dict->objectForKey(&n1);
    };

    T operator =(const T value) {
        create((N1Object*)value);
        return value;
    };

    T operator ->() {
        return T();
    };
};


#endif //N1THREADSINGLETON_2006_10_29
